package com.tff.gmall.search.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.tff.gmall.annotations.LoginRequired;
import com.tff.gmall.beans.*;
import com.tff.gmall.service.PmsBaseAttrInfoService;
import com.tff.gmall.service.SearchService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * @author Fafa Tuo
 * @version 1.0
 * @date 2021/7/1 22:28
 */
@Controller
public class SearchController {

    @Reference
    private SearchService searchService;
    @Reference
    private PmsBaseAttrInfoService baseAttrInfoService;

    @RequestMapping(value = "index")
    @LoginRequired(logSuccess = false)
    public String index(){
        return "index";
    }

    @RequestMapping(value = "list.html")
    public String list(PmsSearchParam pmsSearchParam, ModelMap modelMap){
        System.out.println("pmsSearchParam = " + pmsSearchParam.toString());

        //调用搜索服务，返沪搜索结果
        List<PmsSearchSkuInfo> list = searchService.list(pmsSearchParam);

        modelMap.put("skuLsInfoList",list);

        //抽去检索结果所包含的平台属性值集合
        Set<String> valueIdSet = new HashSet<>();
        for (PmsSearchSkuInfo pmsSearchSkuInfo:list) {
            List<PmsSkuAttrValue> skuAttrValueList = pmsSearchSkuInfo.getSkuAttrValueList();
            if(skuAttrValueList != null){
                for (PmsSkuAttrValue pmsSkuAttrValue : skuAttrValueList) {
                    String valueId = pmsSkuAttrValue.getValueId();
                    valueIdSet.add(valueId);
                }
            }
        }

        //根据valueId查询mysql得到平台属性的集合列表
        String attrValueList = StringUtils.join(valueIdSet, ",");
        List<PmsBaseAttrInfo> pmsBaseAttrInfoList = baseAttrInfoService.getPmsBaseAttrInfoByValueId(attrValueList);

        //对平台属性进一步处理，去掉当前条件中valueId所在的属性组
        String[] delValueIds = pmsSearchParam.getValueId();
        Iterator<PmsBaseAttrInfo> iterator = pmsBaseAttrInfoList.iterator();
        if (delValueIds != null){
            while (iterator.hasNext()){
                PmsBaseAttrInfo pmsBaseAttrInfo = iterator.next();

                List<PmsBaseAttrValue> valueList = pmsBaseAttrInfo.getAttrValueList();
                for (PmsBaseAttrValue pmsBaseAttrValue : valueList) {
                    String valueId = pmsBaseAttrValue.getId();
                    for (String delValueId : delValueIds) {
                        if(delValueId.equals(valueId)){
                            //删除该属性值所对应的属性组
                            iterator.remove();
                        }
                    }
                }
            }
        }

        modelMap.put("attrList",pmsBaseAttrInfoList);
        String urlParam = getUrlParam(pmsSearchParam);
        modelMap.put("urlParam",urlParam);
        //面包屑关键字
        String keyword = pmsSearchParam.getKeyword();
        if (StringUtils.isNotBlank(keyword)){
            modelMap.put("keyword",keyword);
        }

        return "list";
    }

    private String getUrlParam(PmsSearchParam pmsSearchParam) {
        String keyword = pmsSearchParam.getKeyword();
        String catalog3Id = pmsSearchParam.getCatalog3Id();
        String[] skuAttrValueList = pmsSearchParam.getValueId();

        String urlParam = "";
        if(StringUtils.isNotBlank(keyword)){
            if(StringUtils.isNotBlank(urlParam)){
                urlParam = urlParam+"&";
            }
            urlParam = urlParam+"keyword="+keyword;
        }
        if (StringUtils.isNotBlank(catalog3Id)){
            if(StringUtils.isNotBlank(urlParam)){
                urlParam = urlParam+"&";
            }
            urlParam = urlParam+"catalog3Id="+catalog3Id;
        }
        if(skuAttrValueList != null){
            for (String pmsSkuAttrValue : skuAttrValueList) {
                urlParam = urlParam+"&valueId="+pmsSkuAttrValue;
            }
        }

        return urlParam;
    }
}
